package com.ruoyi.merchant.controller;

import com.ruoyi.common.core.cg.pojo.Group;
import com.ruoyi.common.core.cg.pojo.Template;
import com.ruoyi.merchant.service.TemplateService;
import com.ruoyi.merchant.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    /**
     * 根据负责人ID获取模板列表
     *
     * @param leaderId 负责人ID
     * @return 模板列表
     */
    @RequestMapping("/leader")
    public List<Template> getTemplatesByLeaderId(Integer leaderId) {
        return templateService.findTemplatesByLeaderId(leaderId);
    }


    /**
     * 添加新的模板
     *
     * @param template 要添加的模板对象
     * @return 包含操作结果的ResponseEntity对象
     */
    @RequestMapping("insert")
    public ResponseEntity<Map<String, Object>> addTemplate(@RequestBody Template template) {
        Map<String, Object> result = new HashMap<>();
        try {
            Template savedTemplate = templateService.addTemplate(template);
            result.put("success", true);
            result.put("message", "模板添加成功");
            result.put("data", savedTemplate);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "模板添加失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 将模板关联到指定的负责人
     *
     * @param leaderId   负责人ID
     * @param templateId 模板ID
     * @return 包含操作结果的ResponseEntity对象
     */
    @RequestMapping("/saveById")
    public ResponseEntity<Map<String, Object>> useTemplate(Integer leaderId, Integer templateId) {
        Map<String, Object> result = new HashMap<>();
        try {
            templateService.associateTemplateToLeader(leaderId, templateId);
            result.put("success", true);
            result.put("message", "模板已关联到团长");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "关联失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 将模板删除
     *
     * @param templateId 模板ID
     * @return 包含操作结果的ResponseEntity对象
     */

    @RequestMapping("/delTemplateId")
    public ResponseEntity<Map<String, Object>> deleteTemplate(Integer templateId) {
        Map<String, Object> result = new HashMap<>();
        try {
            templateService.deleteTemplate(templateId);
            result.put("success", true);
            result.put("message", "模板删除成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "模板删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**3
     * 根据ID 更新模板
     * @param id
     * @param template
     * @return
     */
    @PutMapping("/updateByid")
    public ResponseEntity<Map<String, Object>> updateTemplate(Integer id, @RequestBody Template template) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (!id.equals(template.getId())) {
                result.put("success", false);
                result.put("message", "路径ID与请求体ID不匹配");
                return ResponseEntity.badRequest().body(result);
            }
            Template updatedTemplate = templateService.updateTemplate(template);
            result.put("success", true);
            result.put("message", "模板更新成功");
            result.put("data", updatedTemplate);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "模板更新失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }





    @RequestMapping("/save")
    public Result saveGroup(
            @ModelAttribute Template template,
            @RequestParam(value = "surfacePlotFile", required = false) MultipartFile surfacePlotFile,
            @RequestParam(value = "firstImgFile", required = false) MultipartFile firstImgFile,
            @RequestParam(value = "secondImgFile", required = false) MultipartFile secondImgFile) {
        return templateService.saveGroup(template, surfacePlotFile, firstImgFile, secondImgFile);
    }

    @RequestMapping("/lid")
    public Result queryGroup(int lid){
        List<Template> groups = templateService.queryTemplate(lid);
        return Result.ok(groups);
    }

    @DeleteMapping("/delete")
    public Result deleteGroup(@RequestBody Map<String, Integer> params) {
        Integer id = params.get("id");
        Integer lid = params.get("lid");
        if (id == null || lid == null) {
            return Result.error("参数id或lid不能为空");
        }
        System.out.println("删除模板的id：" + id + "用户的id：" + lid);
        templateService.deleteLeaderGroup(id, lid);
        return Result.ok();
    }
}
